package com.chenfan.filestore.service;

import com.chenfan.ccp.common.exception.ApiException;
import com.chenfan.ccp.common.result.ResultCode;
import com.chenfan.filestore.exception.FileStoreCode;
import com.chenfan.filestore.model.pdf.WaterMaskInfo;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.*;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

/**
 *  pdf处理service
 */
@Service
public class PdfService extends AbastractFileService{

    public void waterMask(WaterMaskInfo waterMaskInfo, HttpServletResponse response){
        //水印参数校验
        checkWaterMaskParam(waterMaskInfo);

        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        }catch (Exception e){
            e.printStackTrace();
            throw new ApiException(ResultCode.FAIL);
        }

        stampByPdfFile(waterMaskInfo, outputStream);
        //获取文件
    }

    /**
     * 水印参数校验
     * @param waterMaskInfo
     */
    private void checkWaterMaskParam(WaterMaskInfo waterMaskInfo) {
        if(Objects.isNull(waterMaskInfo) || Objects.isNull(waterMaskInfo.getFileId())){
            throw new ApiException(FileStoreCode.FILEID_CANNOT_BE_NULL);
        }
        if(Objects.isNull(waterMaskInfo.getWords())){
            throw new ApiException(FileStoreCode.PDF_WATERMARK_WORDS_NOT_NULL);
        }
    }

    private void stampByPdfFile(WaterMaskInfo waterMaskInfo, OutputStream outputStream) {
        try {
            // 添加水印的时候,就已经在outputStream写入了
            final InputStream inputStream = getInputStream(waterMaskInfo.getFileId());
            PdfReader reader = new PdfReader(inputStream);
            PdfStamper stamper = new PdfStamper(reader, outputStream);
            int total = reader.getNumberOfPages() + 1;
            PdfContentByte content;
            //todo 替换字体文件
            BaseFont base = BaseFont.createFont("simfang.ttf", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            PdfGState gs = new PdfGState();
            for (int i = 1; i < total; i++) {
                content = stamper.getOverContent(i);// 在内容上方加水印
                //content = stamper.getUnderContent(i);//在内容下方加水印
                gs.setFillOpacity(0.5f);
                content.setGState(gs);
                content.beginText();
                content.setTextMatrix(0, 0);
                //将文字显示在pdf页面中
                //设置文字颜色
                content.setColorFill(BaseColor.RED);
                //设置文字大小
                content.setFontAndSize(base, 100);
                content.showTextAligned(Element.ALIGN_CENTER, waterMaskInfo.getWords(), 300, 400, 45);
                content.endText();
            }
            stamper.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

